/**
 * Copyright (C) <2023> <Boundivore> <boundivore@foxmail.com>
 * <p>
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the Apache License, Version 2.0
 * as published by the Apache Software Foundation.
 * <p>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * Apache License, Version 2.0 for more details.
 * <p>
 * You should have received a copy of the Apache License, Version 2.0
 * along with this program; if not, you can obtain a copy at
 * http://www.apache.org/licenses/LICENSE-2.0.
 */
import { FC } from 'react';
import { useSearchParams } from 'react-router-dom';
import { t } from 'i18next';
import APIConfig from '@/api/config';
import RequestHttp from '@/api';
import { App } from 'antd';
import { NodeDropdownButton } from './nodeDropdownButton';
import { MessageInstance } from 'antd/es/message/interface';
interface ReverseSyncConfigProps {
	messageApi: MessageInstance;
	callback: () => void;
}
const ReverseSyncConfig: FC<ReverseSyncConfigProps> = ({ messageApi, callback }) => {
	const [searchParams] = useSearchParams();
	const id = searchParams.get('id');
	const serviceName = searchParams.get('name');
	const { modal } = App.useApp();
	const reverseSync = (nodeId: string) => {
		modal.confirm({
			content: '您确认反向同步该节点的所有配置文件吗？',
			onOk: async () => {
				const api = APIConfig.reverseSyncConfig;
				const params = {
					ClusterId: id,
					NodeId: nodeId,
					ServiceName: serviceName
				};
				const { Code } = await RequestHttp.post(api, params);
				if (Code === '00000') {
					messageApi.success(t('messageSuccess'));
					callback && callback();
				}
			}
		});
	};
	return (
		<>
			<NodeDropdownButton buttonText="反向同步" callBack={reverseSync} />
		</>
	);
};

export default ReverseSyncConfig;
